package cn.wjdiankong.levideo.activity;

import android.view.GestureDetector;
import android.view.MotionEvent;
import cn.wjdiankong.levideo.utils.Logger;

/**
 * 播放器的手势监听
 */
public class LevideoGestureListener extends GestureDetector.SimpleOnGestureListener {

	private int maxDistance = 50;
	private int minDistance = 20;
	private int minVelocity = 0;
	
	@Override
	public boolean onDown(MotionEvent e) {
		return false;
	}

	@Override
	public void onShowPress(MotionEvent e) {
	}

	@Override
	public boolean onSingleTapUp(MotionEvent e) {
		mListener.click();
		return false;
	}

	@Override
	public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
		if (distanceX < -maxDistance && Math.abs(distanceY) < minDistance) {
			Logger.log("向右手势");
			mListener.right();
		} else if (distanceX > maxDistance && Math.abs(distanceY) < minDistance){
			Logger.log("向左手势");
			mListener.left();
		}else if (distanceY <-maxDistance && Math.abs(distanceX) < minDistance){
			Logger.log("向下手势");
			mListener.bottom();
		} else if (distanceY > maxDistance && Math.abs(distanceX) < minDistance){
			Logger.log("向上手势");
			mListener.top();
		}
		return true;
	}


	@Override
	public void onLongPress(MotionEvent e) {
	}

	@Override
	public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
		if(Math.abs(velocityY) > minVelocity){
			float disX = e1.getX() - e2.getX();
			float disY = e1.getY() - e2.getY();
			if (disX > maxDistance && Math.abs(disY) < minDistance) {
				Logger.log("向左手势");
				mListener.left();
			} else if (-disX > maxDistance && Math.abs(disY) < minDistance) {
				Logger.log("向右手势");
				mListener.right();
			} else if (disY > maxDistance && Math.abs(disX) < minDistance) {
				Logger.log("向上手势");
				mListener.top();
			} else if (disY > maxDistance && Math.abs(disX) < minDistance) {
				Logger.log("向下手势");
				mListener.bottom();
			}
		}
		return true;
	}
	
	public interface DirectionListener{
		void top();
		void bottom();
		void left();
		void right();
		void click();
	}
	
	public DirectionListener mListener;
	public void setDirectionListener(DirectionListener listener){
		mListener = listener;
	}

}
